import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 平台级期间定义
 * @date: 2018-7-10


 */
import { getResponse, createPagination } from 'utils/utils';
import { searchHeader, searchLine, savePeriodHeader as _savePeriodHeader, searchPeriodRule as _searchPeriodRule, savePeriod as _savePeriod } from "../services/periodService";
export default {
  namespace: 'period',
  state: {
    periodHeader: {
      // TabPane: "期间定义"
      list: [],
      pagination: {},
      periodData: [] // 期间维护

    },
    periodLine: {
      // TabPane: "期间查询"
      list: [],
      pagination: {}
    }
  },
  effects: {
    searchPeriodHeader: /*#__PURE__*/_regeneratorRuntime.mark(function searchPeriodHeader(_ref, _ref2) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function searchPeriodHeader$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(searchHeader, payload);

            case 4:
              result = _context.sent;
              result = getResponse(result);

              if (!result) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  periodHeader: {
                    list: result.content,
                    pagination: createPagination(result),
                    periodData: [],
                    refDetial: {}
                  }
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, searchPeriodHeader);
    }),
    searchPeriodLine: /*#__PURE__*/_regeneratorRuntime.mark(function searchPeriodLine(_ref3, _ref4) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function searchPeriodLine$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.next = 4;
              return call(searchLine, payload);

            case 4:
              result = _context2.sent;
              result = getResponse(result);

              if (!result) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  periodLine: {
                    list: result.content,
                    pagination: createPagination(result)
                  }
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, searchPeriodLine);
    }),
    savePeriodHeader: /*#__PURE__*/_regeneratorRuntime.mark(function savePeriodHeader(_ref5, _ref6) {
      var payload, call, saveData, result;
      return _regeneratorRuntime.wrap(function savePeriodHeader$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              saveData = payload.saveData;
              _context3.next = 5;
              return call(_savePeriodHeader, saveData);

            case 5:
              result = _context3.sent;
              return _context3.abrupt("return", getResponse(result));

            case 7:
            case "end":
              return _context3.stop();
          }
        }
      }, savePeriodHeader);
    }),
    searchPeriodRule: /*#__PURE__*/_regeneratorRuntime.mark(function searchPeriodRule(_ref7, _ref8) {
      var payload, call, put, periodSetId, periodHeader, result;
      return _regeneratorRuntime.wrap(function searchPeriodRule$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call, put = _ref8.put;
              periodSetId = payload.periodSetId, periodHeader = payload.periodHeader;
              _context4.next = 5;
              return call(_searchPeriodRule, {
                periodSetId: periodSetId
              });

            case 5:
              result = _context4.sent;
              result = getResponse(result);

              if (!result) {
                _context4.next = 10;
                break;
              }

              _context4.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  periodHeader: _objectSpread(_objectSpread({}, periodHeader), {}, {
                    periodData: _toConsumableArray(result)
                  })
                }
              });

            case 10:
            case "end":
              return _context4.stop();
          }
        }
      }, searchPeriodRule);
    }),
    savePeriod: /*#__PURE__*/_regeneratorRuntime.mark(function savePeriod(_ref9, _ref10) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function savePeriod$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(_savePeriod, payload);

            case 4:
              result = _context5.sent;
              return _context5.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, savePeriod);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref11) {
      var payload = _ref11.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};